/*
 * Licensed to the Tai Ping Jin Ke
 *
 * Copyright (c) 2022 .
 * All rights reserved.
 * 项目名称：太平金科-基础组件封装层-ElasticSearch-缺省实现
 * 版权说明：本软件属太平金融科技服务(上海)有限公司所有，在未获得太平金融科技服务(上海)有限公司正式授权情况下，任何企业和个人，不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。
 */
package com.taipingframework.boot.es.autoconfigure;

import com.taipingframework.boot.es.facade.DefaultReactorElasticAccess;
import com.taipingframework.boot.es.facade.ElasticAccess;
import com.taipingframework.boot.es.facade.DefaultElasticAccess;
import com.taipingframework.boot.es.facade.IndexAdmin;
import com.taipingframework.boot.es.facade.ReactorElasticAccess;
import com.taipingframework.boot.es.facade.ReactorIndexAdmin;
import com.taipingframework.boot.es.properties.ElasticProperty;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.reactive.ReactiveElasticsearchClient;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ReactiveElasticsearchTemplate;

@Slf4j
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ElasticsearchRestTemplate.class})
@AutoConfigureAfter({ElasticsearchDataAutoConfiguration.class})
public class ElasticAutoConfiguration {

    /**
     * Elasticsearch
     */
    @Bean(initMethod = "init")
    @ConfigurationProperties(prefix = "application.elastic")
    public ElasticProperty elasticProperty() {
        return new ElasticProperty();
    }

    /**
     * 索引库管理
     */
    @Bean
    public IndexAdmin indexAdmin(RestHighLevelClient restHighLevelClient) {
        return new IndexAdmin(restHighLevelClient);
    }

    /**
     * 索引库管理 - 异步非阻塞
     */
    @Bean
    public ReactorIndexAdmin reactorIndexAdmin(ReactiveElasticsearchClient reactiveElasticsearchClient) {
        return new ReactorIndexAdmin(reactiveElasticsearchClient);
    }

    /**
     * 简化ES相关的CRUD操作
     */
    @Bean
    public ElasticAccess elasticAccess(ElasticsearchRestTemplate elasticsearchRestTemplate) {
        return new DefaultElasticAccess(elasticsearchRestTemplate);
    }

    /**
     * 简化ES相关的CRUD操作 - 异步非阻塞
     */
    @Bean
    public ReactorElasticAccess reactorElasticAccess(ReactiveElasticsearchTemplate reactiveElasticsearchTemplate) {
        return new DefaultReactorElasticAccess(reactiveElasticsearchTemplate);
    }

}
